package pa165.dominatingspecies.frontend.environment;

import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.tos.EnvironmentTo;

import pa165.dominatingspecies.service.impl.EnvironmentServiceImpl;

/**
 *
 * @author Stanislau Dubrouski
 */
@PreAuthorize("hasRole('ADMIN')")
public class DeleteEnvironmentLink extends Link<Void> {

    @SpringBean(name = "environmentService")
    private EnvironmentServiceImpl envService;
    private final EnvironmentTo env;

    public DeleteEnvironmentLink(String componentId, EnvironmentTo env) {
        super(componentId);
        this.env = env;
    }

    @Override
    public void onClick() {
        boolean thrown = false;
        try {
            envService.deleteEnvironment(env);
        } catch (DataAccessException ex) {
            thrown = true;
            getSession().warn(new StringResourceModel("envRemoveFailed", this, null, new Object[]{env.getId()}).getString());
            setResponsePage(EnvironmentsPage.class);
        }
        if (!thrown) {
            getSession().info(new StringResourceModel("envRemoveSucc", this, null, new Object[]{env.getId()}).getString());
            setResponsePage(EnvironmentsPage.class);
        }
    }
}
